﻿using NRules.Fluent.Dsl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.TaskScheduling
{
    /// <summary>
    /// 制样任务
    /// </summary>
    public class SamplePreparationTaskService : TaskSchedulingAppService, ISamplePreparationTaskService
    {
        private IRepository<SamplePreparationTask, string> _samplePreparationTaskRepository;
        private IRepository<SamplePreparationPlan, string> _samplePreparationPlanRepository;

        public SamplePreparationTaskService(IRepository<SamplePreparationTask, string> samplePreparationTaskRepository
            ,IRepository<SamplePreparationPlan, string> samplePreparationPlanRepository)
        {
            _samplePreparationTaskRepository = samplePreparationTaskRepository;
            _samplePreparationPlanRepository = samplePreparationPlanRepository;
        }

        /// <summary>
        /// 获取制样任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<SamplePreparationTaskDto>> GetListAsync(SamplePreparationTaskInput input)
        {
            var iQuery = await _samplePreparationTaskRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(input.Begntime != null, s => s.TaskAssgnTime >= input.Begntime);
            iQuery = iQuery.WhereIf(input.Endtime != null, s => s.TaskAssgnTime <= input.Endtime.Value.AddDays(1));
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.ExecStas), s => s.ExecStas == input.ExecStas);
            iQuery = iQuery.OrderByDescending(i => i.LastOperTime);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<SamplePreparationTask>, List<SamplePreparationTaskDto>>(iQuery.ToList());

                return new PagedResultDto<SamplePreparationTaskDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<SamplePreparationTask>, List<SamplePreparationTaskDto>>(items.ToList());

                return new PagedResultDto<SamplePreparationTaskDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 创建制样任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<SamplePreparationTaskDto> PostAsync(SamplePreparationTaskCreateOrUpdateDto input)
        {
            var iQuery = await _samplePreparationTaskRepository.GetQueryableAsync();
            var existSamplePreparationTask = iQuery.FirstOrDefault(i => i.Id == input.Id);

            if (existSamplePreparationTask != null)
            {
                existSamplePreparationTask.SampleCode = input.SampleCode;
                existSamplePreparationTask.LastOperTime = DateTime.Now;
                existSamplePreparationTask = await _samplePreparationTaskRepository.UpdateAsync(existSamplePreparationTask);
                var itemDto = ObjectMapper.Map<SamplePreparationTask, SamplePreparationTaskDto>(existSamplePreparationTask);

                return itemDto;
            }
            else
            {
                SamplePreparationTask samplePreparationTask = new SamplePreparationTask(input.Id, input.SampleCode);
                samplePreparationTask = await _samplePreparationTaskRepository.InsertAsync(samplePreparationTask);
                var itemDto = ObjectMapper.Map<SamplePreparationTask, SamplePreparationTaskDto>(samplePreparationTask);

                return itemDto;
            }
        }

        /// <summary>
        /// 重新下发制样任务
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<SamplePreparationTaskDto> PutAsync(string id, SamplePreparationTaskCreateOrUpdateDto input)
        { 
            if (id == input.Id)
            {
                var samplePreparationTask = await _samplePreparationTaskRepository.GetAsync(id);
                samplePreparationTask.ReSet(input.SampleCode);
                samplePreparationTask = await _samplePreparationTaskRepository.UpdateAsync(samplePreparationTask);
                var itemDto = ObjectMapper.Map<SamplePreparationTask, SamplePreparationTaskDto>(samplePreparationTask);
                return itemDto;
            }
            else
            {
                SamplePreparationTask samplePreparationTask = new SamplePreparationTask(input.Id, input.SampleCode);
                samplePreparationTask = await _samplePreparationTaskRepository.InsertAsync(samplePreparationTask);
                var itemDto = ObjectMapper.Map<SamplePreparationTask, SamplePreparationTaskDto>(samplePreparationTask);
                return itemDto;
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="samplePreparationTaskId"></param>
        /// <returns></returns>
        public async Task<List<SamplePreparationPlanDto>> GetPlanListAsync(string samplePreparationTaskId)
        {
            var iQuery = await _samplePreparationPlanRepository.GetQueryableAsync();
            iQuery = iQuery.Where(i => i.Id == samplePreparationTaskId);
            var itemsDto = ObjectMapper.Map<List<SamplePreparationPlan>, List<SamplePreparationPlanDto>>(iQuery.ToList());

            return itemsDto;
        }
    }
}
